# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy
import datetime


class SearchspiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass


# 字符串转换时间方法
def date_convert(value):
    try:
        create_date = datetime.datetime.strptime(value, "%Y/%m/%d").date()
    except Exception as e:
        create_date = datetime.datetime.now().date()
    return create_date


class JobBoleItem(scrapy.Item):
    url_object_id = scrapy.Field()
    title = scrapy.Field()
    url = scrapy.Field()
    thumbnail = scrapy.Field()
    images_path = scrapy.Field()
    create_time = scrapy.Field()
    praise_nums = scrapy.Field()
    fav_nums = scrapy.Field()
    comment_nums = scrapy.Field()
    content = scrapy.Field()
    tags = scrapy.Field()

    def get_insert_sql(self):
        insert_sql = """
            insert into jobbole(url_object_id, title, url, thumbnail, images_path, create_time, praise_nums, fav_nums, comment_nums, content, tags)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE title=VALUES(title), content=VALUES(content), praise_nums=VALUES(praise_nums), fav_nums=VALUES(fav_nums), comment_nums=VALUES(comment_nums)
        """
        params = (
            self["url_object_id"],
            self["title"],
            self["url"],
            self["thumbnail"],
            self["images_path"],
            self["create_time"],
            self["praise_nums"],
            self["fav_nums"],
            self["comment_nums"],
            self["content"],
            self["tags"]
        )
        return insert_sql, params
